﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;
using System.Windows.Resources;
using System.IO.IsolatedStorage;
using System.IO;
using System.Globalization;
using System.Xml.Linq;
using System.Collections.Generic;
using lutianqi.Model;

namespace lutianqi.Helpers
{
    //访问和存储XML文件的处理器
    public static class XMLProcessor
    {
        private static string ConfigFile = "Config.xml";//模板
        public static string GetWeatherIconFileName(string ThemeName, string WeatherName)
        {
            string result = string.Empty;
            StreamResourceInfo r = Application.GetResourceStream(new Uri("Images/WeatherIcon/" + ThemeName + "/ThemeConfig.xml", UriKind.Relative));
            using (XmlReader reader = XmlReader.Create(r.Stream))
            {
                while (reader.Read())
                {
                    if ((reader.Name == "Icon") && (reader.GetAttribute("Weather") == WeatherName))
                    {
                        result = reader.ReadInnerXml();
                    }
                }
            }
            return result;
        }

        public static void InitialConfig()
        {
            using (IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isoStorage.FileExists(ConfigFile))
                {
                    using (var isoStream = new IsolatedStorageFileStream(ConfigFile, FileMode.Create, isoStorage))
                    {
                        using (var writer = new BinaryWriter(isoStream))
                        {
                            StreamResourceInfo sri = Application.GetResourceStream(new Uri("Images/" + ConfigFile, UriKind.Relative));

                            using (BinaryReader reader = new BinaryReader(sri.Stream))
                            {
                                long len = sri.Stream.Length;
                                for (long i = 0; i < len; i++)
                                {
                                    writer.Write(reader.ReadByte());
                                }
                            }
                        }
                    }
                }
            }
        }

        public static string ReadCurrentTheme()
        {
            string result = string.Empty;
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ConfigFile))
                {
                    IsolatedStorageFileStream fileStream = storage.OpenFile(ConfigFile, FileMode.Open, FileAccess.ReadWrite);
                    XDocument _doc = XDocument.Load(fileStream);
                    result = _doc.Element("config").Element("CurrentTheme").Attribute("ThemeName").Value;
                    fileStream.Dispose();
                }
            }
            return result;
        }

        public static void SetCurrentTheme(string NewThemeName)
        {
            using ( IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ConfigFile))
                {
                    IsolatedStorageFileStream fileStream = storage.OpenFile(ConfigFile, FileMode.Open, FileAccess.ReadWrite);
                    XDocument _doc = XDocument.Load(fileStream);
                    fileStream.Dispose();
                    _doc.Element("config").Element("CurrentTheme").Attribute("ThemeName").Value = NewThemeName;
                    storage.DeleteFile(ConfigFile);
                    IsolatedStorageFileStream file = storage.CreateFile(ConfigFile);
                    _doc.Save(file);
                    file.Dispose();
                }
            }
        }

        public static List<string> GetThemeList()
        {
            List<string> result = new List<string>();
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ConfigFile))
                {
                    IsolatedStorageFileStream fileStream = storage.OpenFile(ConfigFile, FileMode.Open, FileAccess.ReadWrite);
                    XDocument _doc = XDocument.Load(fileStream);
                    foreach (XElement node in _doc.Element("config").Elements("ThemeList").Nodes())
                    {
                        result.Add(node.Attribute("ThemeName").Value);
                    }
                    fileStream.Dispose();
                }
            }
            return result;
        }

        public static List<FavoriteItem> GetFavoriteItemList()
        {
            List<FavoriteItem> result = new List<FavoriteItem>();
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ConfigFile))
                {
                    IsolatedStorageFileStream fileStream = storage.OpenFile(ConfigFile, FileMode.Open, FileAccess.ReadWrite);
                    XDocument _doc = XDocument.Load(fileStream);
                    foreach (XElement node in _doc.Element("config").Elements("Favorite").Nodes())
                    {
                        string[] tpstrarry = node.Attribute("Background").Value.Split(new char[]{','});
                        SolidColorBrush tpbg = new SolidColorBrush(Color.FromArgb(255, (byte)(int.Parse(tpstrarry[0])), (byte)(int.Parse(tpstrarry[1])), (byte)(int.Parse(tpstrarry[2]))));
                        result.Add(new FavoriteItem() { CityCode = node.Attribute("CityCode").Value, CityName = node.Attribute("CityName").Value, bgColor = tpbg });
                    }
                    fileStream.Dispose();
                }
            }
            return result;
        }

        public static void SetFavoriteColor(string cityCode)
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ConfigFile))
                {
                    IsolatedStorageFileStream fileStream = storage.OpenFile(ConfigFile, FileMode.Open, FileAccess.ReadWrite);
                    XDocument _doc = XDocument.Load(fileStream);
                    fileStream.Dispose();
                    foreach (XElement node in _doc.Element("config").Elements("Favorite").Nodes())
                    {
                        if (node.Attribute("CityCode").Value == cityCode)
                        {
                            node.Attribute("Background").Value = CreateRandomColor();
                        }
                    }
                    storage.DeleteFile(ConfigFile);
                    IsolatedStorageFileStream file = storage.CreateFile(ConfigFile);
                    _doc.Save(file);
                    file.Dispose();
                }
            }
        }

        public static void DelFav(string cityCode)
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ConfigFile))
                {
                    IsolatedStorageFileStream fileStream = storage.OpenFile(ConfigFile, FileMode.Open, FileAccess.ReadWrite);
                    XDocument _doc = XDocument.Load(fileStream);
                    fileStream.Dispose();
                    foreach (XElement node in _doc.Element("config").Elements("Favorite").Nodes())
                    {
                        if (node.Attribute("CityCode").Value == cityCode)
                        {
                            node.Remove();
                        }
                    }
                    storage.DeleteFile(ConfigFile);
                    IsolatedStorageFileStream file = storage.CreateFile(ConfigFile);
                    _doc.Save(file);
                    file.Dispose();
                }
            }
        }

        public static void AddFav(string cityCode,string aCityName)
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(ConfigFile))
                {
                    IsolatedStorageFileStream fileStream = storage.OpenFile(ConfigFile, FileMode.Open, FileAccess.ReadWrite);
                    XDocument _doc = XDocument.Load(fileStream);
                    fileStream.Dispose();
                    XElement tp = new XElement("FavoriteItem");
                    tp.SetAttributeValue("CityCode",cityCode);
                    tp.SetAttributeValue("CityName",aCityName);
                    tp.SetAttributeValue("Background",CreateRandomColor());
                    _doc.Element("config").Element("Favorite").Add(tp);
                    storage.DeleteFile(ConfigFile);
                    IsolatedStorageFileStream file = storage.CreateFile(ConfigFile);
                    _doc.Save(file);
                    file.Dispose();
                }
            }
        }

        public static string CreateRandomColor()
        {
            Random rnd = new Random();
            return rnd.Next(256).ToString() + "," + rnd.Next(256).ToString() + "," + rnd.Next(256).ToString();
        }
    }
}
